package com.webcamtracker.ui.color;

import com.webcamtracker.configuration.RootPref;
import com.webcamtracker.image.color.ColorSampleFactory;
import com.webcamtracker.image.color.ColorSpaceConversions;
import com.webcamtracker.image.color.hsv.HSVColor;
import com.webcamtracker.image.color.hsv.HSVColorSampler;
import com.webcamtracker.image.color.rgb.RGBColor;
import com.webcamtracker.ui.UITracker;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.prefs.Preferences;

public class UIColorFilterPanel extends JPanel {
    private UITracker uiVideoMonitor;
    private JSlider readSlider;
    private JSlider greenSlider;
    private JSlider blueSlider;


    public UIColorFilterPanel() {

    }

    public UIColorFilterPanel(UITracker uiVideoTracker) {
        this();
        this.uiVideoMonitor = uiVideoTracker;
        setupUI();
    }


    public void setupUI() {
        this.setLayout(new GridLayout(1, 4));

        this.add(setUpColor());
        this.add(setupThreshold());
        this.add(setupColorSpace());

    }

    private JPanel setupColorSpace() {
        JPanel result = new JPanel();
        CompoundBorder border = BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Color Space"),
                BorderFactory.createEmptyBorder(5, 5, 5, 5));
        result.setBorder(border);
        final JComboBox box = new JComboBox(ColorSampleFactory.getValues());
        box.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String type = box.getSelectedItem().toString();
                System.out.println("type = " + type);
                RootPref.getColorPref().put(RootPref.KEY_COLOR_TYPE, type);

            }
        });
        box.setSelectedItem(RootPref.getColorPref().get(RootPref.KEY_COLOR_TYPE, RootPref.DEF_VALUE_COLOR_TYPE));
        result.add(box);
        return result;
    }

    private JPanel setupButtom() {
        JPanel result = new JPanel();
        result.setLayout(new FlowLayout());
        JButton pickColor = new JButton("Pick Color");
        pickColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                uiVideoMonitor.getVideo().getVideoManager().getVideoComponent().addMouseListener(new ColorPicker());
            }
        });
        result.add(pickColor);
        return result;
    }

    protected JPanel setupThreshold() {
        JPanel result = new JPanel();
        CompoundBorder border = BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Filter Properties"),
                BorderFactory.createEmptyBorder(5, 5, 5, 5));
        result.setBorder(border);
        result.setLayout(new BorderLayout());

        final Preferences colorPref = RootPref.getColorPref();
        JSlider euclideanDistance = new JSlider(JSlider.HORIZONTAL, 1, 120, (int) colorPref.getDouble(RootPref.KEY_TRESHOLD, RootPref.DEF_VALUE_TRESHOLD));
        final JLabel distance = new JLabel(RootPref.KEY_TRESHOLD + euclideanDistance.getValue());

        euclideanDistance.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider) e.getSource();
                colorPref.putDouble(RootPref.KEY_TRESHOLD, slider.getValue());
                distance.setText(RootPref.KEY_TRESHOLD + slider.getValue());
            }
        });
        result.add(distance, BorderLayout.WEST);
        result.add(euclideanDistance, BorderLayout.CENTER);

        return result;
    }

    private JPanel setUpColor() {
        JPanel result = new JPanel();


        CompoundBorder border = BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("RGB Color Selection"),
                BorderFactory.createEmptyBorder(5, 5, 5, 5));
        result.setBorder(border);
        result.setLayout(new BorderLayout());
        final JPanel colorSample = new JPanel();
        colorSample.setLayout(new GridLayout(3, 1));
        final JLabel rValue = new JLabel("R:" + getSearchColor().getRed());
        final JLabel gValue = new JLabel("G:" + getSearchColor().getGreen());
        final JLabel bValue = new JLabel("B:" + getSearchColor().getBlue());
        colorSample.add(rValue);
        colorSample.add(gValue);
        colorSample.add(bValue);
        JPanel labels = new JPanel();
        labels.setLayout(new GridLayout(3, 1));
        JLabel read = new JLabel("Read :");
        JLabel green = new JLabel("Green :");
        JLabel blue = new JLabel("Blue :");
        labels.add(read);
        labels.add(green);
        labels.add(blue);
        JPanel slidersPanel = new JPanel();
        slidersPanel.setLayout(new GridLayout(3, 1));

        readSlider = createColorSlider();
        readSlider.setValue(getSearchColor().getRed());
        readSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider) e.getSource();
                getSearchColor().setRead(slider.getValue());
                colorSample.setBackground(new Color(slider.getValue(), colorSample.getBackground().getGreen(), colorSample.getBackground().getBlue()));
                rValue.setText("R:" + slider.getValue());
                HSVColor hsvColor = (HSVColor) (new HSVColorSampler()).getColorFrom(getSearchColor());
                System.out.println("HSV Color: " + hsvColor);
            }
        });

        greenSlider = createColorSlider();
        greenSlider.setValue(getSearchColor().getGreen());
        greenSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider) e.getSource();
                getSearchColor().setGreen(slider.getValue());
                colorSample.setBackground(new Color(colorSample.getBackground().getRed(), slider.getValue(), colorSample.getBackground().getBlue()));
                gValue.setText("G:" + slider.getValue());
                HSVColor hsvColor = (HSVColor) (new HSVColorSampler()).getColorFrom(getSearchColor());
                System.out.println("HSV Color: " + hsvColor);
            }
        });

        blueSlider = createColorSlider();
        blueSlider.setValue(getSearchColor().getBlue());
        blueSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider) e.getSource();
                getSearchColor().setBlue(slider.getValue());
                colorSample.setBackground(new Color(colorSample.getBackground().getRed(), colorSample.getBackground().getGreen(), slider.getValue()));
                bValue.setText("B:" + slider.getValue());
                HSVColor hsvColor = (HSVColor) (new HSVColorSampler()).getColorFrom(getSearchColor());
                System.out.println("HSV Color: " + hsvColor);
            }
        });
        JPanel colorManager = new JPanel(new BorderLayout());
        slidersPanel.add(readSlider);
        slidersPanel.add(greenSlider);
        slidersPanel.add(blueSlider);

        colorManager.add(slidersPanel, BorderLayout.CENTER);
        colorSample.setBackground(new Color(getSearchColor().getRed(), getSearchColor().getGreen(), getSearchColor().getBlue()));
        JPanel main = new JPanel();
        main.add(labels, BorderLayout.WEST);
        main.add(colorManager, BorderLayout.CENTER);
        main.add(colorSample, BorderLayout.EAST);
        JPanel jPanel = setupButtom();
        result.add(jPanel, BorderLayout.NORTH);
        result.add(main, BorderLayout.CENTER);
        return result;

    }

    private RGBColor getSearchColor() {

        return RootPref.getSearchColor();
    }

    private JSlider createColorSlider() {
        JSlider readSlider = new JSlider();
        readSlider.setMaximum(255);
        readSlider.setMinimum(0);
        return readSlider;
    }


    private class ColorPicker implements MouseListener {


        public void mouseClicked(MouseEvent e) {

        }

        public void mousePressed(MouseEvent e) {
            RGBColor previousColor = getSearchColor();
            int rgbColor = uiVideoMonitor.getVideo().getVideoManager().getImage().getRGB(e.getX(), e.getY());
            RGBColor color = new RGBColor(rgbColor);
            HSVColor hsvColor = ColorSpaceConversions.fromRGBtoHSV(color);
            HSVColor hsvPrevious = (HSVColor) (new HSVColorSampler()).getColorFrom(previousColor);
            System.out.println("Distance con el previous: " + hsvColor.getEuclideanDistance(hsvPrevious));
            System.out.println("HSV : " + hsvColor);
            readSlider.setValue(color.getRed());
            blueSlider.setValue(color.getBlue());
            greenSlider.setValue(color.getGreen());
        }

        public void mouseReleased(MouseEvent e) {

        }

        public void mouseEntered(MouseEvent e) {

        }

        public void mouseExited(MouseEvent e) {

        }
    }


}
